//
//  QNLimitTextField.m
//  QNView
//
//  Created by QNRZMac on 15/5/10.
//  Copyright (c) 2015年 Sunline. All rights reserved.
//

#import "JFLimitTextField.h"
#import <JFBase/JFBase.h>

@implementation JFLimitTextField


#pragma mark - life Circle


-(instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:nil];
        self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:nil];
        self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}


-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    _textView.frame = CGRectMake(0, 5, rect.size.width, rect.size.height-20);
    
    NSString * limitStr = [@(_curLimitNum/2) stringValue];
    CGSize size = [limitStr sizeWithAttributes:@{NSFontAttributeName:self.font}];
    
    self.cornerLabel.text = [@(_curLimitNum/2) stringValue];
    self.cornerLabel.frame = CGRectMake(rect.size.width -size.width-5, rect.size.height-size.height-2 , size.width,size.height);
}


#pragma mark - UITextViewDelegate

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([self.delegate respondsToSelector:@selector(limitTextFieldShouldBeginEditing:)]) {
        return [self.delegate limitTextFieldShouldBeginEditing:self];
    }
    return YES;
}


-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([self.delegate respondsToSelector:@selector(limitTextFieldShouldEndEditing:)]) {
        return [self.delegate limitTextFieldShouldEndEditing:self];
    }
    return YES;
}
#pragma mark - OverRide

- (BOOL)resignFirstResponder{
    [super resignFirstResponder];
    return [self.textView resignFirstResponder];
}

#pragma mark - Notification Respone

- (void)textDidChange{
    _curLimitNum = _limitNum  - [self getToInt:self.textView.text];
    _cornerLabel.text = [@(_curLimitNum/2) stringValue];
    
    if (_curLimitNum<0) {
        _cornerLabel.textColor = [UIColor redColor];
        _isOutOfRange = YES;
        self.cornerLabel.text = [@(0/2) stringValue];
    }else{
        _cornerLabel.textColor = [UIColor lightGrayColor];
        _isOutOfRange = NO;
        self.cornerLabel.text = [@(_curLimitNum/2) stringValue];
    }
    [self outOfRangeRemind:_isOutOfRange];
    NSString * limitStr = [@(_curLimitNum/2) stringValue];
    CGSize size = [limitStr sizeWithAttributes:@{NSFontAttributeName:self.font}];
    
 
    self.cornerLabel.frame = CGRectMake(self.frame.size.width -size.width-5, self.frame.size.height-size.height-2 , size.width,size.height);
}

- (int)convertToInt:(NSString*)strtemp
{
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
        
    }
    return strlength;
}

//得到中英文混合字符串长度 方法2
- (int)getToInt:(NSString*)strtemp

{
    int legthCount = 0;
    for(int i = 0 ;i<strtemp.length;i++){
        unichar c = [strtemp characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FFF)
        {
            legthCount +=2 ;
        }
        else
        {
            legthCount += 1;
        }
    }
    return legthCount;
}


- (void)outOfRangeRemind:(BOOL)isOutOfRange{
    if ([self.delegate respondsToSelector:@selector(limitTextFieldTextIsOutOfRange:)]) {
        [self.delegate limitTextFieldTextIsOutOfRange:isOutOfRange];
    }
}

#pragma Setter & Getter

-(void)setLimitNum:(NSInteger)limitNum{
    _limitNum = limitNum;
    _curLimitNum = limitNum;
    [self textDidChange];
    [self.textView limitTextLength:limitNum numberOfChineseChar:2];
}


-(UILabel *)cornerLabel{
    if (!_cornerLabel) {
        _cornerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
        _cornerLabel.textColor = [UIColor lightGrayColor];
        _cornerLabel.font = self.font;
        [self addSubview:_cornerLabel];
    }
    return _cornerLabel;
}


-(UITextField *)textView{
    if (!_textView) {
        _textView = [[UITextField alloc] init];
        _textView.delegate = self;
        _textView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [self addSubview:_textView];
    }
    return _textView;
}


-(void)setText:(NSString *)text{
    self.textView.text = text;
    [self textDidChange];
}

-(NSString *)text{
    return self.textView.text;
}

-(UIFont *)font{
    if (!_font) {
        _font = [UIFont systemFontOfSize:12];
    }
    return _font;
}



@end
